<?php
/***************************************************************************
 *
 * Copyright (c) 2010 , Inc. All Rights Reserved
 * $Id$
 *
 **************************************************************************/

/**
 * @file ValidationFuncRef.php
 * @author niulei(niulei@)
 * @date 2010/05/17 18:58:28
 * @version $Revision$
 * @brief
 *
 **/
final class ValidationFuncRef {
	static function valid_range($str_length, $max = 0, $min = 0) {
		if ($max == 0) { //只有min的情况；
			if ($str_length < $min) {
				return false;
			} else {
				return true;
			}
		}
		if ($str_length <= $max && $str_length >= $min) {
			return true;
		}
		return false;
	}

	static function valid_encode($str, $encode) {
		return mb_check_encoding ( $str, $encode );
	}

	static function valid_value($str, $arr_value, $flag) {
		if ($flag & Validation::FLAG_CASE_IGNORE) { //忽略大小写;
			foreach ( $arr_value as $value ) {
				if (strcasecmp ( $value, $str ) == 0) {
					return true;
				}
			}
		} else { //区分大小写
			if (in_array ( $str, $arr_value )) {
				return true;
			}
		}
		return false;
	}

	static function valid_format($str, $pattern) {
		$reg_pattern = array ('options' => array ('regexp' => $pattern ) );
		if (filter_var ( $str, FILTER_VALIDATE_REGEXP, $reg_pattern ) === false) {
			return false;
		} else {
			return true;
		}
	}

	static function valid_content($str, $flag) {
		if ($flag & Validation::FLAG_ALLOW_HIGH) { //allow high;
		} else {
			if (self::has_high_character ( $str )) { //deny high;
				return false;
			}
		}
		if ($flag & Validation::FLAG_ALLOW_LOW) { //allow low;
		} else { //deny low;
			if ($flag & Validation::FLAG_ALLOW_ENTER) { //allow enter
				if (self::has_high_and_enter_character ( $str )) {
					return true;
				} else {
					return false;
				}
			} else {
				if (self::has_low_character ( $str )) {
					return false;
				}
			}
		}
		return true;
	}

	static function valid_str_length($str, $arr_option) {
		if (isset ( $arr_option ['encode'] )) {
			$encode_in = $arr_option ['encode'];
			if (strcasecmp ( $encode_in, 'gbk' ) != 0 && strcasecmp ( $encode_in, 'utf-8' ) != 0) {
				return false;
			}
			$str_len = mb_strlen ( $str, $encode_in );
		} else {
			$str_len = mb_strlen ( $str, 'gbk' );
		}
		if (isset ( $arr_option ['byte_length'] ) && $arr_option ['byte_length'] == true) {
			$str_len = strlen ( $str );
		}
		if (isset ( $arr_option ['max_range'] )) {
			if ($arr_option ['max_range'] == 0) {
				return false;
			}
			$min_range = 0;
			if (isset ( $arr_option ['min_range'] )) {
				$min_range = $arr_option ['min_range'];
			}
			if (! self::valid_range ( $str_len, $arr_option ['max_range'], $min_range )) {
				return false;
			}
		} elseif (isset ( $arr_option ['min_range'] )) {
			$max_range = 0;
			if (! self::valid_range ( $str_len, $max_range, $arr_option ['min_range'] )) {
				return false;
			}
		}
		if (isset ( $arr_option ['length_array'] )) {
			if (! self::length_in_array ( $str_len, $arr_option ['length_array'] )) {
				return false;
			}
		}
		return true;
	}

	//处理含gbk, utf-8汉字的字符串
	static function valid_letter($str) {
		$enc = mb_detect_encoding($str,array('UTF-8','ASCII'));
		if ('UTF-8' != $enc) {
			$str = mb_convert_encoding($str,'UTF-8',$enc);
			if (false === $str) {
				return false;
			}
		}
		$pattern = '/[\x00-\x1f\x21-\x2c\x2e-\x2f\x3a-\x40\x5b-\x5e\x60\x7b-\x7f]/';
		//存在非法字符，返回false
		if (self::valid_format ( $str, $pattern )) {
			return false;
		}
		return true;
	}

	static function valid_birth($str)
	{
		if (strlen($str) == 15){
			$birth_year  = substr($str, 6,  2);   //get year
			$birth_month = substr($str, 8,  2);   //get month;
			$birth_day   = substr($str, 10, 2);    //get day;
			$birth_year19 = '19' . $birth_year;
			$birth_year20 = '20' . $birth_year;

			$input_time_stamp19 = mktime(0,0,0, $birth_month, $birth_day, $birth_year19);
			$input_time_stamp20 = mktime(0,0,0, $birth_month, $birth_day, $birth_year20);
				
			$input_date19 = $birth_year19 . '-' . $birth_month . '-' .$birth_day;
			$input_date20 = $birth_year20 . '-' . $birth_month . '-' .$birth_day;
			$stand_date19 = date('Y-m-d', $input_time_stamp19);
			$stand_date20 = date('Y-m-d', $input_time_stamp20);

			if (($input_date19 != $stand_date19) &&
			($input_date20 != $stand_date20)){
				return false;
			}else{
				return true;
			}
		}else{     //18 byte;
			$birth_year  = substr($str, 6,  4);    //get year;
			$birth_month = substr($str, 10, 2);    //get month;
			$birth_day   = substr($str, 12, 2);    //get day;
			$cur_time_stamp     = time();          //2008-08-21 20:09:10;

			//1.判断时间的正确性;
			$input_time_stamp = mktime(0,0,0, $birth_month, $birth_day, $birth_year);
			$input_date = $birth_year . '-' . $birth_month . '-' .$birth_day;
			$stand_date = date('Y-m-d', $input_time_stamp);

			if ($input_date != $stand_date){
				return false;   //输入日期不合法；
			}

			//2.判断时间范围;
			$dis = ($cur_time_stamp - $input_time_stamp) / (3600 * 24 * 365);

			if ( $dis < 16){
				return false;
			}

			if ($input_date < '1900-01-01'){
				return false;
			}
			return true;
		}
	}

	static function length_in_array($length, $arr_length) {
		return in_array ( $length, $arr_length );
	}

	static function is_number($str) {
		$pattern = '/^[0-9]+$/';
		return self::valid_format ( $str, $pattern );
	}

	static function has_high_and_enter_character($str) {
		$pattern = '/^[\x09\x0a\x0d\x20-\xff]*$/';
		$ret = self::valid_format ( $str, $pattern );
		return $ret;
	}
	static function has_low_character($str) {
		$pattern = '/[\x00-\x19]/'; //至少存在一个
		$ret = self::valid_format ( $str, $pattern );
		return $ret;
	}
	static function has_high_character($str) {
		$pattern = '/[\x80-\xff]/'; //至少存在一个
		$ret = self::valid_format ( $str, $pattern );
		return $ret;
	}

	static function input_empty($str) {
		if (trim ( $str ) === '' || $str === NULL) {
			return true;
		}
		return false;
	}
}
